//
//  HomeOriginalView.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/28.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit
import SnapKit

//原创微博视图
class HomeOriginalView: UIView {
    
    //记录原创微博的 底部约束
    var originalViewBottomConstraint: Constraint?
    
    //数据模型
    var statusViewModel:ZKStatusViewModel?{
        
        didSet{
            
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                //赋值头像
                userImgView.sd_setImage(with: URL(string:imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //赋值昵称
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            //赋值微博内容
            contentLabel.text = statusViewModel?.status?.text
            
            //赋值会员等级
            membershipImgView.image = statusViewModel?.membershipImg
            //赋值认证类型
            avatarImgView.image = statusViewModel?.avatarImg
            //赋值数据来源
            sourceLabel.text = statusViewModel?.sourceContent
            //赋值时间
            timeLabel.text = statusViewModel?.timeContent

            //卸载之前的底部约束
            originalViewBottomConstraint?.deactivate()
            //赋值配图
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                
                //表示有配图
                pictureView.isHidden = false
                //重新创建底部约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(10).constraint
                })
                //绑定数据
                pictureView.picUrls = picUrls
                
            }else{
                
                //没有配图
                //隐藏视图
                pictureView.isHidden = true
                //重新创建底部约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp.bottom).offset(10).constraint
                })
            }
        }
    }
    
    //用户头像
    fileprivate lazy var userImgView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型
    fileprivate lazy var avatarImgView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    fileprivate lazy var nameLabel:UILabel = {
        
        let label = UILabel(font: 14, textColor: UIColor.black)
        label.text = "我是xxx"
        
        return label
    }()
    //时间
    fileprivate lazy var timeLabel:UILabel = {
        
        let label = UILabel(font: 12, textColor: UIColor.darkGray)
        label.text = "刚刚"
        
        return label
    }()
    //微博来源
    fileprivate lazy var sourceLabel:UILabel = {
        
        let label = UILabel(font: 12, textColor: UIColor.darkGray)
        label.text = "来自:XXX"
        
        return label
    }()
    //微博等级
    fileprivate lazy var membershipImgView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    //微博内容
    fileprivate lazy var contentLabel:UILabel = {
        
        let label = UILabel(font: 12, textColor: UIColor.black)
        label.text = "【马布里：底薪签约罗斯简直就是在犯罪】前NBA球星斯蒂芬-马布里在得知2011年的常规赛MVP德里克-罗斯与骑士队达成了1年210万美元的加盟协议后，他难以置信地表示，罗斯所签的这份合同简直就是在“犯罪”。另外，他还表示，如果罗斯有意转战CBA赛场，那他起码能拿到四五百万美元的年薪"
        label.numberOfLines = 0
        
        return label
    }()
    //原创微博配图
    fileprivate lazy var pictureView: HomePictureView = {
        
        let collectionView = HomePictureView()
        //配图颜色和转发微博颜色一样
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()



    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //添加控件设置约束
    private func setupUI(){
        backgroundColor = UIColor.white
        
        userImgView.layer.masksToBounds = true
        userImgView.layer.cornerRadius = 40 / 2
        
        addSubview(userImgView)
        addSubview(avatarImgView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(membershipImgView)
        addSubview(contentLabel)
        addSubview(pictureView)
        userImgView.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(10)
            
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        avatarImgView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImgView.snp.trailing).offset(-5)
            
            make.centerY.equalTo(userImgView.snp.bottom).offset(-5)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImgView)
            make.leading.equalTo(userImgView.snp.trailing).offset(10)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(userImgView)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp.trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        membershipImgView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp.trailing).offset(10)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImgView.snp.bottom).offset(10)
            make.leading.equalTo(userImgView)
            make.trailing.equalTo(self).offset(-10)
        }
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.leading.equalTo(contentLabel)
            
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
        }
        
        //注意!高度不能写死
        self.snp.makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    }
}


